package com.example.mylibrary.di.module;

import android.text.TextUtils;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.mylibrary.db.bean.Constants;
import com.example.mylibrary.db.gen.Constant;
import com.example.mylibrary.net.Api;
import com.example.mylibrary.net.HttpLoggingInterceptor;
import com.example.mylibrary.net.SSLSocketClient;
import com.example.mylibrary.net.UrlConstant;
import com.example.mylibrary.utils.DeviceUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;


/**
 * 网络请求Module
 *
 * @author mark.liu
 */
@Module
public class ApiModule {

    @Singleton
    @Provides
    OkHttpClient.Builder provideClientBuilder() {
        return new OkHttpClient.Builder();
    }

    @Singleton
    @Provides
    Interceptor provideInterceptor() {
        // 打印请求信息的拦截器
        return new HttpLoggingInterceptor();
    }

    @Provides
    @Singleton
    OkHttpClient provideApiOkHttpClient(OkHttpClient.Builder builder, Interceptor intercept) {
        String macAddress = DeviceUtils.getMacAddress();
        if (TextUtils.isEmpty(macAddress)) {
            try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("/sys/class/net/eth0/address")))) {
                macAddress = input.readLine();
                LogUtils.e("以太网 MAC Address: " + macAddress);
            } catch (IOException ex) {
                LogUtils.e("以太网Mac获取失败ex: " + ex);
                macAddress = "02:00:00:00:00:00";
            }
        }
        String finalMacAddress = TextUtils.isEmpty(macAddress) ? TextUtils.isEmpty(DeviceUtil.getIntelMacAddress()) ? "" : DeviceUtil.getIntelMacAddress() : macAddress;
        return builder
//                .cache(cache)
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(chain -> {
                    Request build = chain.request().newBuilder()
                            .addHeader(Constant.HMD, SPUtils.getInstance().getString(Constant.HMD, "0"))
                            .addHeader(Constant.HSZ, SPUtils.getInstance().getString(Constant.HSZ, "0"))
                            .addHeader(Constant.HV, SPUtils.getInstance().getString(Constant.HV, "0"))
                            .addHeader(Constant.HSV, SPUtils.getInstance().getString(Constant.HSV, "0"))
                            .addHeader(Constant.HAM, SPUtils.getInstance().getString(Constant.HAM, "0"))
                            .addHeader(Constant.HLO, SPUtils.getInstance().getString(Constant.HLO, "0"))
                            .addHeader(Constant.HLA, SPUtils.getInstance().getString(Constant.HLA, "0"))
                            .addHeader(Constant.HM, finalMacAddress)
                            .addHeader(Constant.HM2, DeviceUtil.getIntelMacAddress())
                            .addHeader(Constant.AK, Constants.APPKEY)
                            .addHeader(Constant.MK, DeviceUtil.getDeviceSerialNum())
                            .addHeader(Constant.HS, DeviceUtil.getSerialNumber())
                            .build();
                    return chain.proceed(build);
                })
                .addInterceptor(intercept)
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
    }

    @Singleton
    @Provides
    Retrofit.Builder provideRetrofitBuilder() {
        return new Retrofit.Builder();
    }

    //外部存储
//    File httpCacheDirectory = new File(BaseApplication.getContext().getExternalCacheDir(), "responses");
    //设置缓存 50M
//    int cacheSize = 200 * 1024 * 1024;
//    Cache cache = new Cache(httpCacheDirectory, cacheSize);


    @Singleton
    @Provides
    Retrofit provideRetrofit(Retrofit.Builder builder, OkHttpClient okHttpClient) {
        return builder

                .client(okHttpClient)
                .baseUrl(UrlConstant.DOMAIN)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Provides
    @Singleton
    Api provideApi(Retrofit retrofit) {
        return new Api(retrofit);
    }
}